#include "cjson/cJSON.h"
#include "log/log.h"
#include <stdlib.h>
int main(int argc, char const *argv[])
{
    int age = 18;
    char *name = "zhangsan";
    // 创建json对象
    cJSON *json = cJSON_CreateObject();
    // 添加属性
    cJSON_AddStringToObject(json, "name", name);
    cJSON_AddNumberToObject(json, "age", age);
    // 打印json对象
    char *json_str = cJSON_PrintUnformatted(json);
    log_debug("%s\n", json_str);
    // 解析json对象
    cJSON *json2 = cJSON_Parse(json_str);
    cJSON *name_json = cJSON_GetObjectItem(json2, "name");
    cJSON *age_json = cJSON_GetObjectItem(json2, "age");
    if (name_json == NULL || age_json == NULL)
    {
        log_error("json parse error\n");
        return -1;
    }
    // 打印属性
    log_debug("name:%s, age:%d\n", name_json->valuestring, age_json->valueint);
    // 释放json对象
    cJSON_Delete(json);
    cJSON_Delete(json2);
    free(json_str);

    return 0;
}
